package com.jinxinol.basic.system.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysOrgan;
import com.jinxinol.basic.system.domain.SysPosition;
import com.jinxinol.basic.system.domain.SysRole;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysOrganService;
import com.jinxinol.basic.system.service.ISysPositionService;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.common.PageModel;
import com.jinxinol.core.util.encryption.MD5;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author Chenbing
 * @email wwwchenbing@gmail.com
 * @description 用户管理Action
 * @date 2012-7-12
 */
public class UserAction extends BaseAction {
	private static final long serialVersionUID = -8004959989512432543L;
	private static final Log logger = ContextHelper.getLogger(UserAction.class);
	
	private SysUser user = new SysUser();
	private PageModel<SysUser> pm = new PageModel<SysUser>();
	private List<SysUser> list = new ArrayList<SysUser>();
	private Integer id;//用户ID
	private SysOrgan org = new SysOrgan();
	
	//职务列表
	private List<SysPosition> posList = new ArrayList<SysPosition>();
	private List<SysRole> roleList_yes = new ArrayList<SysRole>();
	private List<SysRole> roleList_no = new ArrayList<SysRole>();
	
	@Resource 
	ISysUserService userService;
	@Resource
	ISysOrganService orgService;
	@Resource
	ISysPositionService posService;
	
	public UserAction(){}
	
	/**
	 * 添加用户
	 * */
	public String insertUser(){
		try {
			SysUser cu = ContextHelper.getCurrentUser();
			user.setCreatedby(cu.getId());
			user.setUstate("1"); //创建时默认为"启用"状态
			user.setUsource("1");
			user.setUunit(0);
			MD5 d = new MD5();
			user.setPword(d.getMD5ofStr("111111"));//用户密码MD5加密
			
			userService.insert(user);
			
			return "insertUser_success";
		} catch (Exception e) {
			logger.error("新增用户"+user.getRname() +"失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 删除用户
	 * */
	public String deleteUser(){
		try {
			userService.delete(user);
			logger.info("删除用户成功！");
			return SUCCESS;
		} catch (Exception e) {
			logger.error("删除用户出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改用户信息
	 * */
	public String updateUser(){
		try {
			userService.update(user);
			return SUCCESS;
		} catch (Exception e) {
			logger.error("修改用户"+user.getRname()+"出现异常！", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改用户状态
	 * */
	public void updateUserState(){
		try {
			userService.update(user);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("修改用户"+user.getRname()+"状态出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
			jsonObject.put(RESULT_MSG, "改变用户状态失败，请联系管理员！");
		}
		this.pushJsonResult();
	}
	
	/**
	 * 重置用户密码
	 * */
	public void resetPassword(){
		try {
			MD5 d = new MD5();
			user.setPword(d.getMD5ofStr("111111"));//用户密码MD5加密
			userService.update(user);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("修改用户"+user.getRname()+"状态出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
			jsonObject.put(RESULT_MSG, "重置用户密码失败，请联系管理员！");
		}
		this.pushJsonResult();
	}
	
	/**
	 * 按ID查询用户
	 * */
	public String selectByUserID(){
		try {
			user=userService.findById(user.getId());
			return "update";
		} catch (Exception e) {
			logger.error("按ID查询用户出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 按条件查询用户
	 * */
	public String selectUser(){
		try {
			pm = userService.findOnPage(user, pm);
			return "list";
		} catch (Exception e) {
			logger.error("按条件查询用户出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 为选择用户界面提供数据
	 * @return
	 */
	public String selectUser4Choose(){
		try {
			//区域id不为空
			if(org.getId()!=null && !org.getId().equals(0)){
				user.setOrg_id(org.getId());
				list = userService.find(user);
				org = orgService.findById(org.getId());
			}else if(org.getO_name()!=null){
				user.setOrg_name(org.getO_name());
				user.setOrg_id(null);
				list = userService.find(user);
				org.setO_name(org.getO_name());
				
			}
			
			
			return "list_choose_single";
		} catch (Exception e) {
			logger.error("按条件查询用户出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 检查用户合法性出现异常
	 * */
	public void checkUser(){
		try {
			String valid = "true";
			if(user.getCheckUser() != null && user.getCheckUser().equals("insert")){
				//新增用户时对登录名进行唯一性验证
				List<SysUser> ul = userService.find(user);
				valid = ul.size()==0 ? "true" : "false";
			}else if(user.getCheckUser() != null && user.getCheckUser().equals("update")){
				//更新用户时对登录名进行唯一性验证
				Integer userId = user.getId();
				user.setId(null);
				List<SysUser> ul = userService.find(user);
				
				for(SysUser su : ul){
					if(!su.getId().equals(userId)){
						valid = "false";
						break;
					}
				}
			}
			
			jsonObject.put("valid", valid);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("检查用户合法性出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		this.pushJsonResult();
	}
	
	/**
	 * 进入用户职务维护页面
	 * @return
	 * @throws Exception
	 */
	public String gotoUserPosition()throws Exception{
		try {
			//查询用户基本信息
			user = userService.findById(user.getId());
			
			//查询用户已有副职
			List<SysPosition> tempList = userService.selectUserPosition(user);
			user.setPositionList(tempList);
			
			//查询用户所在部门的职务
			SysPosition pos = new SysPosition();
			pos.setOrg_id(user.getOrg_id());
			posList = posService.find(pos);
			
			return "user_position";
		} catch (Exception e) {
			logger.error("进入用户职务维护页面失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 保存用户职务信息
	 * @return
	 * @throws Exception
	 */
	public void saveUserPosition(){
		try {
			
			userService.saveUserPosition(user);
			
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("保存用户职务信息出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		this.pushJsonResult();
	}
	
	/**
	 * 进入用户角色维护页面
	 * @return
	 * @throws Exception
	 */
	public String gotoUserRole()throws Exception{
		try {
			//查询用户基本信息
			user = userService.findById(user.getId());
			
			//查询用户已有角色
			roleList_yes = userService.selectUserRole(user);
			
			//查询用户没有的角色
			roleList_no = userService.selectOtherRole(user);
			
			return "user_role";
		} catch (Exception e) {
			logger.error("进入用户角色维护页面失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 保存用户角色信息
	 * @return
	 * @throws Exception
	 */
	public void saveUserRole(){
		try {
			userService.saveUserRole(user);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("保存用户职务信息出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		this.pushJsonResult();
	}
	
	public SysUser getUser() {
		return user;
	}
	public void setUser(SysUser user) {
		this.user = user;
	}
	public PageModel<SysUser> getPm() {
		return pm;
	}
	public void setPm(PageModel<SysUser> pm) {
		this.pm = pm;
	}
	public List<SysUser> getList() {
		return list;
	}
	public void setList(List<SysUser> list) {
		this.list = list;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public List<SysPosition> getPosList() {
		return posList;
	}
	public void setPosList(List<SysPosition> posList) {
		this.posList = posList;
	}

	public List<SysRole> getRoleList_yes() {
		return roleList_yes;
	}

	public void setRoleList_yes(List<SysRole> roleList_yes) {
		this.roleList_yes = roleList_yes;
	}
	
	public List<SysRole> getRoleList_no() {
		return roleList_no;
	}

	public void setRoleList_no(List<SysRole> roleList_no) {
		this.roleList_no = roleList_no;
	}

	public SysOrgan getOrg() {
		return org;
	}

	public void setOrg(SysOrgan org) {
		this.org = org;
	}
}
